/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.unnison.framework.junit;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.google.inject.matcher.Matchers;

import com.unnison.framework.client.GuitEntryPoint;
import com.unnison.framework.client.appcontroller.AppController;
import com.unnison.framework.client.command.CommandService;
import com.unnison.framework.client.eventbus.EventBus;
import com.unnison.framework.client.eventbus.EventBusImpl;
import com.unnison.framework.client.logging.Logger;
import com.unnison.framework.client.place.PlaceManager;

public class GuitTestHelper implements AppController {

    private static class TestModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(EventBus.class).to(EventBusImpl.class).in(Singleton.class);
            bind(PlaceManager.class).to(PlaceManagerMock.class).in(Singleton.class);
            bind(CommandService.class).to(CommandServiceMock.class).in(Singleton.class);
            bind(Logger.class).to(LoggerMock.class).in(Singleton.class);
            bindListener(Matchers.any(), new GuitTestListener());
        }
    }

    private final Injector injector;

    public GuitTestHelper(Module module) {
        injector = Guice.createInjector(new TestModule(), module);
        injector.injectMembers(module);
        GuitEntryPoint.setAppController(this);
    }

    public <T> T get(Class<T> clazz) {
        return injector.getInstance(clazz);
    }

    @Override
    public CommandService getCommandService() {
        return get(CommandService.class);
    }

    @Override
    public EventBus getEventBus() {
        return get(EventBus.class);
    }

    @Override
    public Logger getLogger() {
        return get(Logger.class);
    }

    @Override
    public PlaceManager getPlaceManager() {
        return get(PlaceManager.class);
    }

    @Override
    public void inject() {
        // DO NOTHING
    }
}
